<template>
  <div id="questionsView">
    <a-form :model="searchParams" layout="inline">
      <a-form-item field="title" label="名称" style="min-width: 240px">
        <a-input v-model="searchParams.title" placeholder="请输入名称" />
      </a-form-item>
      <a-form-item field="tags" label="标签" style="min-width: 240px">
        <a-input-tag v-model="searchParams.tags" placeholder="请输入标签" />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="doSubmit">搜索</a-button>
      </a-form-item>
    </a-form>
    <a-divider size="0" />
    <a-table
      :ref="tableRef"
      :columns="columns"
      :data="dataList"
      :pagination="{
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
      }"
      @page-change="onPageChange"
    >
      <template #tags="{ record }">
        <a-space wrap>
          <a-tag v-for="(tag, index) of record.tags" :key="index" color="green">
            {{ tag }}
          </a-tag>
        </a-space>
      </template>
      <template #acceptedRate="{ record }">
        <span
          :class="[
            'accepted-rate',
            record.submitNum
              ? record.acceptedNum / record.submitNum > 0.7
                ? 'accepted-rate-high'
                : record.acceptedNum / record.submitNum > 0.3
                ? 'accepted-rate-medium'
                : 'accepted-rate-low'
              : '',
          ]"
        >
          {{
            `${
              record.submitNum
                ? (record.acceptedNum / record.submitNum) * 100
                : "0"
            }% (${record.acceptedNum}/${record.submitNum})`
          }}
        </span>
      </template>
      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY-MM-DD") }}
      </template>
      <template #optional="{ record }">
        <a-space>
          <a-button type="primary" @click="toQuestionPage(record)">
            做题
          </a-button>
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import {
  Question,
  QuestionControllerService,
  QuestionQueryRequest,
} from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import * as querystring from "querystring";
import { useRouter } from "vue-router";
import moment from "moment";

const tableRef = ref();

const dataList = ref([]);
const total = ref(0);
const searchParams = ref<QuestionQueryRequest>({
  title: "",
  tags: [],
  pageSize: 8,
  current: 1,
});

const loadData = async () => {
  const res = await QuestionControllerService.listQuestionVoByPageUsingPost(
    searchParams.value
  );
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("加载失败，" + res.message);
  }
};

/**
 * 监听 searchParams 变量，改变时触发页面的重新加载
 */
watchEffect(() => {
  loadData();
});

/**
 * 页面加载时，请求数据
 */
onMounted(() => {
  loadData();
});

// {id: "1", title: "A+ D", content: "新的题目内容", tags: "["二叉树"]", answer: "新的答案", submitNum: 0,…}

const columns = [
  {
    title: "题号",
    dataIndex: "id",
  },
  {
    title: "题目名称",
    dataIndex: "title",
  },
  {
    title: "标签",
    slotName: "tags",
  },
  {
    title: "通过率",
    slotName: "acceptedRate",
  },
  {
    title: "创建时间",
    slotName: "createTime",
  },
  {
    slotName: "optional",
  },
];

const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};

const router = useRouter();

/**
 * 跳转到做题页面
 * @param question
 */
const toQuestionPage = (question: Question) => {
  router.push({
    path: `/view/question/${question.id}`,
  });
};

/**
 * 确认搜索，重新加载数据
 */
const doSubmit = () => {
  // 这里需要重置搜索页号
  searchParams.value = {
    ...searchParams.value,
    current: 1,
  };
};
</script>

<style scoped>
#questionsView {
  max-width: 1280px;
  margin: 0 auto;
  padding: 24px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.arco-form {
  margin-bottom: 16px;
}

.arco-form-item-label {
  font-weight: 500;
  color: #4e5969;
}

.arco-input,
.arco-input-tag,
.arco-btn {
  border-radius: 4px;
}

.arco-input:hover,
.arco-input-tag:hover {
  background-color: #f7f8fa;
}

.arco-table {
  border-radius: 8px;
  margin-top: 16px;
}

.arco-table-th {
  background-color: #f7f8fa !important;
  font-weight: 500;
  color: #4e5969;
}

.arco-table-tr:hover .arco-table-td {
  background-color: #f7f8fa !important;
}

/* 标签样式 */
.arco-tag {
  margin-right: 8px;
  margin-bottom: 4px;
  border-radius: 4px;
}

/* 通过率样式 */
.accepted-rate {
  font-weight: 500;
}

.accepted-rate-high {
  color: #00b42a;
}

.accepted-rate-medium {
  color: #ff7d00;
}

.accepted-rate-low {
  color: #f53f3f;
}

.arco-btn {
  font-weight: 500;
  transition: all 0.2s;
}

.arco-btn-primary {
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.arco-btn-primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.arco-pagination {
  margin-top: 20px;
}

/* 移动端适配 */
@media (max-width: 768px) {
  #questionsView {
    padding: 16px;
  }

  .arco-form-item {
    width: 100%;
    margin-bottom: 12px;
  }

  .arco-input,
  .arco-input-tag {
    width: 100% !important;
  }
}
</style>
